<template>
  <div class="card-list">
    <div class="m-card" v-for="(item, index) in list" :key="index">
      <div class="card" @click="selectItem(item.vid)">
        <el-image class="m-cover" :src="item.img | coverUrl" lazy></el-image>
        <div class="count">
          <p class="title">{{ item.title }}</p>
        </div>
      </div>
      <div v-if="item.uid == userInfo.Uid">
          <div v-if="!item.show" style="float:left">
            <i class="el-icon-lock" />
            <span>未公开</span>
          </div>
          <div style="float:right">
              <el-button icon="el-icon-edit-outline" type="text" @click="modifyItem(item, index)">修改</el-button>
              <el-button icon="el-icon-delete" type="text"  @click="showDeleteDialog(item,index)">删除</el-button>
          </div>
      </div>
    </div>
    <el-dialog title="提示" :visible.sync="dialogDeleteVisible" width="30%">
      <span>该操作将会永久删除该视频，是否确认删除？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogDeleteVisible = false">取 消</el-button>
        <el-button type="primary" @click="deleteVideo()">确 定</el-button>
     </span>
    </el-dialog>
    <el-dialog title="修改视频信息" :visible.sync="dialogFormVisible">
      <el-form :model="modifyForm" :rules="rules" ref="modifyForm">
        <el-form-item label="标题" prop="title">
          <el-input v-model="modifyForm.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="简介" prop="content">
          <el-input v-model="modifyForm.content" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="公开">
          <el-switch v-model="modifyForm.isShow"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="beforemodifyVideo('modifyForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Cookies from 'js-cookie'
import { coverUrl } from "@/utils/url";
import {deleteVideo,modifyMyVideo} from "@/api/video"
export default {
  props: {
    list: {
      type: Array,
      default: null,
    },
  },
  data() {
    return {
      dialogFormVisible: false,
      dialogDeleteVisible:false,
      index: "", //数组索引，用于修改后在页面中修改
      deleteId: {
        id: "",
      },
      modifyForm: {
        id: "",
        title: "",
        content: "",
        isShow: Boolean,
      },
      rules: {
        title: [{ required: true, message: "请输入标题", trigger: "blur" }],
        content: [{ required: true, message: "请输入简介", trigger: "blur" }],
      },
    };
  },
  methods: {
    showDeleteDialog(item,index){
      this.deleteId.id = item.vid;
      this.index = index;
      this.dialogDeleteVisible = true;
    },
    deleteVideo(){
      deleteVideo(this.deleteId).then((res) => {
        if (res.data.code === 200) {
          this.$message.success(res.data.msg);
          this.list.splice(this.index, 1);
          this.dialogDeleteVisible = false;
        } else {
          this.$message.error(res.data.msg);
        }
      });
    },
    modifyItem(item, index) {
      this.modifyForm.id = item.vid;
      this.modifyForm.title = item.title;
      this.modifyForm.content = item.content;
      this.modifyForm.isShow = item.show;
      this.dialogFormVisible = true;
      this.index = index;
    },
    beforemodifyVideo(userForm) {
      this.$refs[userForm].validate((valid) => {
        if (valid) {
          this.modifyMyVideo();
        } else {
          this.$message.error("请检查输入的数据");
          return;
        }
      });
    },
    modifyMyVideo() {
      modifyMyVideo(this.modifyForm).then((res) => {
        if (res.data.code === 200) {
          this.$message.success("修改成功");
          this.dialogFormVisible = false;
          //修改本地数据
          this.list[this.index].title = this.modifyForm.title;
          this.list[this.index].content = this.modifyForm.content;
          this.list[this.index].show = this.modifyForm.isShow;
        } else {
          this.$message.error(res.data.msg);
        }
      });
    },
    selectItem(vid) {
      this.$router.push({ name: "video", params: { vid: vid } });
    },
  },
  computed: {
    userInfo() {
      if (Cookies.get('userInfo')) {
        return JSON.parse(Cookies.get('userInfo'));
      } else {
        return null;
      }
    },
  },
  filters: {
    coverUrl(name) {
      return coverUrl(name);
    },
  },
};
</script>

<style scoped>
.card-list {
  display: flex;
  flex-wrap: wrap;
}

.m-card {
  display: inline-block;
  width: 25%;
  height: 240px;
  padding: 8px 5px;
  box-sizing: border-box;
}

.card {
  position: relative;
  overflow: hidden;
  border-radius: 3px;
}

.m-cover {
  top: 0;
  left: 0;
  width: 100%;
  height: 180px;
}

.count {
  display: flex;
  justify-content: space-between;
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  padding: 5px 6px;
  height: 80px;
  box-sizing: border-box;
  color: #ffffff;
  background: linear-gradient(0deg, rgba(0, 0, 0, 0.85), transparent);
}

.title {
  font-size: 13px;
  line-height: 16px;
  margin-top: 40px;
  color: #fff;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 1;
  -webkit-box-orient: vertical;
}
</style>